home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 23
/
Aminet 23 (1998)(GTI - Schatztruhe)[!][Feb 1998].iso
/
Aminet
/
disk
/
misc
/
TransADF.lha
/
Source
/
main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-12-06
|
9KB
|
386 lines
/*----------------*/
/* Main procedure */
/*----------------*/
/* COMPILE_LITE takes precedence over COMPILE_RT */
#ifdef COMPILE_LITE
# ifdef COMPILE_RT
# undef COMPILE_RT
# endif /* COMPILE_RT */
#endif /* COMPILE_LITE */
#include <exec/types.h>
#include <exec/ports.h>
#include <exec/io.h>
#ifdef COMPILE_RT
# include <exec/libraries.h>
#endif /* COMPILE_RT */
#include <devices/trackdisk.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/rdargs.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#include "read_disk.h"
#include "write_disk.h"
#ifndef COMPILE_LITE
# include "defl_disk.h"
# include "infl_disk.h"
#endif /* COMPILE_LITE */
#include "util.h"
#include "errors.h"
#include "version.h"
/*---------------------------------*/
/* Global constants and structures */
/*---------------------------------*/
const char breakText[] = "***Break";
const char TA_Template[] = "DRIVE/A,FILE/A,S=START/N,E=END/N,WRITE/S,\
ZLIB/S,GZIP/S,PKZIP/S,NAME/K,LEVEL/N,ADD/S";
struct TA_Args {
STRPTR DRIVE;
STRPTR FILE;
ULONG *START;
ULONG *END;
ULONG WRITE;
ULONG ZLIB;
ULONG GZIP;
ULONG PKZIP;
STRPTR NAME;
ULONG *LEVEL;
ULONG ADD;
};
/*----------------------------*/
/* Standatd Input/Ouput/Error */
/*----------------------------*/
BPTR StdIn, StdOut, StdErr;
STRPTR ProgName;
/*
** Set handles for DOS Library StdIO functions, as well as
** find our command name.
*/
void setStdIO (void)
{
struct Process *Proc = (struct Process *)FindTask (NULL);
StdIn = Proc->pr_CIS; /* Quicker than Input() or Output() */
StdOut = Proc->pr_COS;
StdErr = Proc->pr_CES;
if (!StdErr)
{
/* Use the file handle from the file number of stdio FILE stderr */
StdErr = (BPTR) fdtofh (fileno (stderr));
if (!StdErr) StdErr = StdOut;
}
}
/*-------------------------------------------*/
/* Global variables and associated functions */
/*-------------------------------------------*/
#ifdef COMPILE_RT
/* z.library base pointer */
struct Library *ZBase;
#endif /* COMPILE_RT */
/* args */
struct RDArgs *rdargs;
/* files */
BPTR ADFile;
STRPTR DOSDev;
/* io */
struct MsgPort *diskPort;
struct IOExtTD *diskReq;
BOOL TDOpen;
/*
** Initialise all globals to their default values.
*/
void initGlobals (void)
{
#ifdef COMPILE_RT
ZBase = NULL;
#endif /* COMPILE_RT */
setStdIO();
rdargs = NULL;
ADFile = NULL;
DOSDev = NULL;
diskPort = NULL;
diskReq = NULL;
TDOpen = FALSE;
/* Initialise other modules */
rd_TrackBuf = NULL; /* In read_disk.c */
wd_TrackBuf = NULL; /* In write_disk.c */
#ifndef COMPILE_LITE
dd_TrackBuf = NULL; /* In defl_disk.c */
dd_FileBuf = NULL; /* In defl_disk.c */
id_FileBuf = NULL; /* In infl_disk.c */
id_TrackBuf = NULL; /* In infl_disk.c */
#endif /* COMPILE_LITE */
}
/*
** Exit cleanly.
*/
void cleanExit (ULONG rc, LONG rc2)
{
/* Cleanup other modules */
if (rd_TrackBuf) FreeVec (rd_TrackBuf); /* In read_disk.c */
if (wd_TrackBuf) FreeVec (wd_TrackBuf); /* In write_disk.c */
#ifndef COMPILE_LITE
if (dd_TrackBuf) FreeVec (dd_TrackBuf); /* In defl_disk.c */
if (dd_FileBuf) FreeVec (dd_FileBuf); /* In defl_disk.c */
if (id_FileBuf) FreeVec (id_FileBuf); /* In infl_disk.c */
if (id_TrackBuf) FreeVec (id_TrackBuf); /* In infl_disk.c */
#endif /* COMPILE_LITE */
if (TDOpen)
{
/* Flush any buffers that may be unwritten */
diskReq->iotd_Req.io_Command = CMD_UPDATE;
diskReq->iotd_Req.io_Flags = 0;
DoIO ((struct IORequest *)diskReq);
/* Turn off the motor */
diskReq->iotd_Req.io_Command = TD_MOTOR;
diskReq->iotd_Req.io_Flags = 0;
diskReq->iotd_Req.io_Length = 0;
DoIO ((struct IORequest *)diskReq);
CloseDevice ((struct IORequest *)diskReq);
}
if (diskReq) DeleteIORequest ((APTR)diskReq);
if (diskPort) DeleteMsgPort (diskPort);
if (DOSDev) Inhibit (DOSDev,FALSE);
if (ADFile) Close (ADFile);
if (rdargs) FreeArgs (rdargs);
/* Turn the cursor on */
FPuts (StdOut, "\033[1 p");
Flush (StdOut);
#ifdef COMPILE_RT
if (ZBase) CloseLibrary (ZBase);
#endif /* COMPILE_RT */
SetIoErr (rc2);
exit (rc);
}
/*---------------*/
/* Main function */
/*---------------*/
int main (int argc, char *argv[])
{
struct TA_Args taArgs = {NULL, NULL, NULL, NULL,
FALSE, FALSE, FALSE, FALSE, NULL, NULL, FALSE};
struct ADF_Packet adf_packet = {NULL, NULL, NULL, NULL, NULL, NULL};
LONG diskUnit;
STRPTR ADFileName, origName;
ULONG startTrack = 0, endTrack = 159, cLevel = 6;
ULONG fileType = 0;
BOOL WriteDisk = FALSE;
LONG ADFOpenMode = MODE_NEWFILE; /* Default is to write to file */
BYTE TDError;
LONG DOSError;
/* Initialise any global data */
initGlobals();
ProgName = FilePart (argv[0]);
#ifdef COMPILE_RT
/* Open z.library */
ZBase = OpenLibrary ("z.library",0);
if (!ZBase)
{
FPrintf (StdErr, "%s: Couldn't open z.library.\n", ProgName);
cleanExit (RETURN_FAIL, NULL);
}
#endif /* COMPILE_RT */
/* Analyse arguments */
rdargs = ReadArgs (TA_Template, (ULONG *)&taArgs, NULL);
if (!rdargs)
outputUsage();
diskUnit = Name2Unit (taArgs.DRIVE);
ADFileName = taArgs.FILE;
if (taArgs.START) startTrack = *(taArgs.START) * 2;
if (taArgs.END) endTrack = *(taArgs.END) * 2 + 1;
if (taArgs.WRITE) WriteDisk = TRUE;
#ifdef COMPILE_LITE
if ((taArgs.ZLIB) || (taArgs.GZIP) || (taArgs.PKZIP) ||
(taArgs.NAME) || (taArgs.LEVEL) || (taArgs.ADD))
{
FPrintf (StdErr, "%s: Options ZLIB, GZIP, PKZIP, NAME, LEVEL and ADD\n",
ProgName);
FPuts (StdErr, " are disabled in Lite version.\n");
cleanExit (RETURN_FAIL, NULL);
}
#else /* ifndef COMPILE_LITE */
if (taArgs.ZLIB)
fileType = FT_ZLIB;
else if (taArgs.GZIP)
fileType = FT_GZIP;
else if (taArgs.PKZIP)
fileType = FT_PKZIP;
origName = taArgs.NAME;
if (taArgs.LEVEL)
cLevel = *(taArgs.LEVEL);
if ((taArgs.ADD) && (taArgs.PKZIP))
fileType = FT_PKZIP_ADD;
#endif /* COMPILE_LITE */
if (diskUnit < 0)
{
FPrintf (StdErr, "%s: Error - Invalid drive.\n",ProgName);
cleanExit (RETURN_FAIL, NULL);
}
if ((startTrack < 0) || (endTrack > 159) || (startTrack > endTrack))
{
FPrintf (StdErr, "%s: Invalid Start/End track.\n", ProgName);
cleanExit (RETURN_FAIL, NULL);
}
/* Inhibit DOS access to the drive we'll be operating on */
DOSDev = taArgs.DRIVE;
if ( !Inhibit (DOSDev, DOSTRUE) )
{
DOSError = IoErr();
FPrintf (StdErr, "%s: Error - Couldn't inhibit DOS access to %s.\n",
ProgName, DOSDev);
DOSDev = NULL;
cleanExit (RETURN_FAIL, DOSError);
}
/* Open the Amiga Disk File */
if (WriteDisk) ADFOpenMode = MODE_OLDFILE; /* Reading from file */
else if (fileType == FT_PKZIP_ADD) ADFOpenMode = MODE_READWRITE;
ADFile = Open (ADFileName, ADFOpenMode);
if (!ADFile)
{
DOSError = IoErr();
FPrintf (StdErr, "%s: Couldn't open %s for %s - ", ProgName, ADFileName,
(WriteDisk ? "input" : "output"));
reportDOSError (DOSError);
cleanExit (RETURN_FAIL, DOSError);
}
/* Attempt to create a MsgPort */
diskPort = CreateMsgPort();
if (!diskPort)
{
FPrintf (StdErr,
"%s: Error - Couldn't create a Message Port for TrackDisk IO.\n",
ProgName);
cleanExit (RETURN_FAIL, NULL);
};
/* Attempt to create an IORequest */
diskReq = (struct IOExtTD *)
CreateIORequest (diskPort, sizeof (struct IOExtTD));
if (!diskReq)
{
FPrintf (StdErr,
"%s: Error - Couldn't create a Request for TrackDisk IO.\n",
ProgName);
cleanExit (RETURN_FAIL, NULL);
}
/* Attempt to open trackdisk.device */
TDError = OpenDevice (TD_NAME, diskUnit, (struct IORequest *)diskReq,NULL);
if (TDError)
{
FPrintf (StdErr, "%s: Couldn't open trackdisk.device - ", ProgName);
reportTDError (TDError);
cleanExit (RETURN_FAIL, NULL);
}
else
TDOpen = TRUE;
/* The trackdisk.device in now open and ready for use */
/* Turn off the cursor */
FPuts (StdOut, "\033[0 p"); Flush (StdOut);
/* Load packet */
adf_packet.diskReq = diskReq;
adf_packet.diskUnit = diskUnit;
adf_packet.ADFile = ADFile;
adf_packet.ADFileName = ADFileName;
adf_packet.startTrack = startTrack;
adf_packet.endTrack = endTrack;
if (WriteDisk)
{
/* We're reading from the file and writing to the disk */
#ifndef COMPILE_LITE
fileType = getFileType (ADFile);
if ((fileType == FT_ZLIB) ||
(fileType == FT_GZIP) ||
(fileType == FT_PKZIP))
inflDisk (&adf_packet, origName, fileType);
else
#endif /* COMPILE_LITE */
writeDisk (&adf_packet);
}
else
{
/* We're reading from the disk and writing to the file */
#ifndef COMPILE_LITE
if (fileType)
deflDisk (&adf_packet, cLevel, origName, fileType);
else
#endif /* COMPILE_LITE */
readDisk (&adf_packet);
}
FPuts (StdOut, "\nDone.\n");
/* Let's get outa here! */
cleanExit (RETURN_OK, NULL);
/* Keep the compiler happy */
return 0;
}